function init(){
    AUTO_SAVE_TIME = 100;
    TIPS_SHOW_TIME = 1000 * 5;
	window._change = window._old = 0;
    var _departments = window._departments || undefined;
    if(_departments){
        var dps = _departments.departments;
        var model = $(".model");
        $.each(dps, function(i){
            var onerow = model.clone();
            onerow.removeClass("model").addClass("model1");
            onerow.children().eq(0).html(i + 1);
            onerow.children().eq(1).html(this.bmmc);
            
            var dataObj = findByBmid(this.bmid);
            var data = [];
            if(dataObj){
                onerow.children().eq(0).append("<input type='hidden' name='{0}_requestId' value='{1}' />".format(this.bmid, dataObj.request_id));
                data.push(dataObj.yfxz);
                data.push(dataObj.xzxn);
                data.push(dataObj.zwgk);
                data.push(dataObj.fwtd);
                data.push(dataObj.qzlj);
                data.push(dataObj.gzcx);
            }
            
            var bmid = this.bmid;
            onerow.find("select").each(function(j){
                $(this).attr("name", "{0}_{1}".format(bmid, j));
                $(this).val(data[j]);
            });
            
            //填充数据
            
            var count = $(".model1").size();
            if(count > 0){
                $(".model1").last().after(onerow);
            } else {
                model.after(onerow);
            }
        });
    }
    
    
    var _data = window._data || undefined;
    if(_data){
        if(!_data.Table.length){
            ajaxSave(function(json){
                location.href = location.href;
            });
        } else {
            $("#suggestion").val(_data.Table[0].suggestion);
            setTimeout(autoSave, AUTO_SAVE_TIME); 
        }
    }
    
	$("select").mouseover(function(){
		
	});
	
    //提交
    $("#btnSubmit").click(function(){
		if(confirm("提交以后就不允许再次修改，确认提交？")){
            $(":button").attr("disabled", true);
			ajaxSave(function(json){
				json = nehnre.parseJSON(json);
				alert("您的测评已经提交，感谢您参与本次测评");
				location.href = location.href;
			}, 1);
		}
    });
    
    $("#btnBack").click(function(){
        location.href = "evaluation.aspx?method=logout";
    });
	/*
	$("select").change(function(){
		window._change = new Date().getTime();
	});
	*/
	$("textarea").keyup(function(){
		window._change = new Date().getTime();
	});
	$("#btnSave").click(function(){
	
		$("#btnSave").attr("disabled", true);
		showTips("正在保存...");
		ajaxSave(function(){
			showTips("于{0}自动保存！".format(new Date().format("hh:mm:ss")));
			$("#btnSave").attr("disabled", false);
			setTimeout(autoSave, AUTO_SAVE_TIME); 
		}); 
	});
}

function findByBmid(bmid){
    var _data = window._data || undefined;
    if(_data){
        _data = _data.Table;
        var obj = null;
        $.each(_data, function(){
            if(this.bmid == bmid){
                obj = this;
            }
        });
        return obj;
    }
}
function ajaxSave(callback, isSubmit){
    var data = $("#myForm").serialize();
    data += "&method=updateData";
	if(isSubmit){
		data += "&is_submit=1";
	}
    $.ajax({
        url:"ashx/evaluation.ashx",
        data:data,
        type:"post",
        success: callback
    });
}
function showTips(con){
	$(".tips").html(con).show();
	clearTimeout(window.handle);
	if(con != "正在保存..."){
		window.handle = setTimeout(function(){
			$(".tips").hide();
		}, TIPS_SHOW_TIME);
	}
}

function autoSave(){
	if(window._nosave){
		return;
	}
	console.log(_change);
	if(window._change > window._old){
		window._old = window._change;
		$("#btnSave").attr("disabled", true);
		showTips("正在保存...");
		ajaxSave(function(){
			showTips("于{0}自动保存！".format(new Date().format("hh:mm:ss")));
			$("#btnSave").attr("disabled", false);
			setTimeout(autoSave, AUTO_SAVE_TIME); 
		}); 
	} else {
		setTimeout(autoSave, AUTO_SAVE_TIME); 
	}
}

$(function(){
    function showload(){
		if ( $.browser.msie && $.browser.version * 1 < 7){
			if(document.documentElement.scrollTop * 1 > 313){
				$("#tableTitle").show();
			} else {
				$("#tableTitle").hide();
			}
		} else {
			if($(this).scrollTop() * 1 > 300){
				$("#tableTitle").show();
			} else {
				$("#tableTitle").hide();
			}
		}
    };
    $(window).bind("resize", showload).bind("scroll", showload); 
});